//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "event\fn_eventObjectiveAssassinate.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_killed_msg","_shared_variable","_killed_code","_evt_idx"];
if (isServer) then {
	DEFAULT_PARAM(_params,0,[]);
	DEFAULT_PARAM(_action_id,1,-1);
	
	_object = GETVAL(_params,0,objNull);
	_killer = GETVAL(_params,1,objNull);
	if (!isnull _object) then {
		_action = [_object,_action_id] call FFUNC(actionGet);
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		_instance_id = [_object] call FFUNC(getInstanceID);
		
		_status = [_object,_action,"ASSASSINATE_KILLED_STATUS",-1] call FFUNC(actionGetLocal);
		if (!isNil "_status" && _status < 0) then {
			[_object,_action,"ASSASSINATE_KILLED_STATUS",1] call FFUNC(actionSetLocal);
			
			_killed_msg = [_parameters, "KILLED_MSG", ""] call CFUNC(hashGet);
			_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
			_killed_code = [_parameters, "KILLED_CODE", ""] call CFUNC(hashGet);

			if (typename _killed_msg == typename "") then {_killed_msg = [_killed_msg];};
			
			_evt_idx = [_object,_action,"ASSASSINATE_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
			if (!isNil "_evt_idx" && _evt_idx >= 0) then {
				_object removeEventHandler ["killed", _evt_idx];
				[_object,_action,"ASSASSINATE_KILLED_EVENT",-1] call FFUNC(actionSetLocal);
			};
		
			if (!isNil "_killed_msg") then {
				if (count _killed_msg > 0) then {[QGVAR(all_message),[_killed_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent);};
			};
			if (!isNil "_shared_variable") then {
				if (_shared_variable != "") then {
					[_instance_id,_shared_variable,!(alive _object)] call FFUNC(setMissionJIPVar);
					[QGVAR(all_var), [_instance_id,_shared_variable,!(alive _object)],THIS_FILE_] call NFUNC(callEvent);
				};
			};
			if (_killed_code != "") then {
				[QGVAR(all_execute),[_killed_code,_object,_action],THIS_FILE_] call NFUNC(callEvent);
			};
		};
	};
} else {
	[QGVAR(s_execute),[QUOTE(FFUNC(eventObjectiveAssassinate)),_this],THIS_FILE_] call NFUNC(callEvent);
	_evt_idx = [_object,_action,"ASSASSINATE_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
	if (!isNil "_evt_idx" && _evt_idx >= 0) then {
		_object removeEventHandler ["killed", _evt_idx];
		[_object,_action,"ASSASSINATE_KILLED_EVENT",-1] call FFUNC(actionSetLocal);
	};
};
TRACE("End");
TMFPROFILERSTOP;